package com.dianping.cat.home.router.transform;
import com.dianping.cat.home.router.IVisitor;
import com.dianping.cat.home.router.entity.DefaultServer;
import com.dianping.cat.home.router.entity.Domain;
import com.dianping.cat.home.router.entity.Group;
import com.dianping.cat.home.router.entity.GroupServer;
import com.dianping.cat.home.router.entity.Network;
import com.dianping.cat.home.router.entity.NetworkPolicy;
import com.dianping.cat.home.router.entity.RouterConfig;
import com.dianping.cat.home.router.entity.Server;
import com.dianping.cat.home.router.entity.ServerGroup;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

public class DefaultNativeParser implements IVisitor {
  private DefaultLinker m_linker;

  public DefaultNativeParser(InputStream in) {
/*  21 */     this.m_linker = new DefaultLinker(true);




/*  26 */     this.m_in = new DataInputStream(in);
  }
  private DataInputStream m_in;

/*  30 */   public static RouterConfig parse(byte[] data) { return parse(new ByteArrayInputStream(data)); }


  public static RouterConfig parse(InputStream in) {
/*  34 */     DefaultNativeParser parser = new DefaultNativeParser(in);
/*  35 */     RouterConfig routerConfig = new RouterConfig();

    try {
/*  38 */       routerConfig.accept(parser);
/*  39 */     } catch (RuntimeException e) {
/*  40 */       if (e.getCause() == null || !(e.getCause() instanceof java.io.EOFException))
      {

/*  43 */         throw e;
      }
    }

/*  47 */     parser.m_linker.finish();
/*  48 */     return routerConfig;
  }



  public void visitDefaultServer(DefaultServer defaultServer) {
    byte tag;
/*  55 */     while ((tag = readTag()) != -1) {
/*  56 */       visitDefaultServerChildren(defaultServer, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDefaultServerChildren(DefaultServer defaultServer, int _field, int _type) {
/*  61 */     switch (_field) {
      case 1:
/*  63 */         defaultServer.setId(readString());
        break;
      case 2:
/*  66 */         defaultServer.setWeight(readDouble());
        break;
      case 3:
/*  69 */         defaultServer.setPort(readInt());
        break;
      case 4:
/*  72 */         defaultServer.setEnable(readBoolean());
        break;
    }
  }



  public void visitDomain(Domain domain) {
    byte tag;
/*  81 */     while ((tag = readTag()) != -1) {
/*  82 */       visitDomainChildren(domain, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitDomainChildren(Domain domain, int _field, int _type) {
/*  87 */     switch (_field) {
      case 1:
/*  89 */         domain.setId(readString());
        break;
      case 33:
/*  92 */         if (_type == 1) {
/*  93 */           Group group = new Group();

          visitGroup(group);
/*  96 */           this.m_linker.onGroup(domain, group); break;
/*  97 */         }  if (_type == 2) {
/*  98 */           for (int i = readInt(); i > 0; i--) {
/*  99 */             Group group = new Group();

/* 101 */             visitGroup(group);
/* 102 */             this.m_linker.onGroup(domain, group);
          }
        }
        break;
    }
  }



  public void visitGroup(Group group) {
    byte tag;
/* 113 */     while ((tag = readTag()) != -1) {
/* 114 */       visitGroupChildren(group, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitGroupChildren(Group group, int _field, int _type) {
/* 119 */     switch (_field) {
      case 1:
/* 121 */         group.setId(readString());
        break;
      case 33:
/* 124 */         if (_type == 1) {
/* 125 */           Server server = new Server();

/* 127 */           visitServer(server);
/* 128 */           this.m_linker.onServer(group, server); break;
/* 129 */         }  if (_type == 2) {
/* 130 */           for (int i = readInt(); i > 0; i--) {
/* 131 */             Server server = new Server();

/* 133 */             visitServer(server);
/* 134 */             this.m_linker.onServer(group, server);
          }
        }
        break;
    }
  }



  public void visitGroupServer(GroupServer groupServer) {
    byte tag;
/* 145 */     while ((tag = readTag()) != -1) {
/* 146 */       visitGroupServerChildren(groupServer, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitGroupServerChildren(GroupServer groupServer, int _field, int _type) {
/* 151 */     switch (_field) {
      case 1:
/* 153 */         groupServer.setId(readString());
        break;
    }
  }



  public void visitNetwork(Network network) {
    byte tag;
/* 162 */     while ((tag = readTag()) != -1) {
/* 163 */       visitNetworkChildren(network, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitNetworkChildren(Network network, int _field, int _type) {
/* 168 */     switch (_field) {
      case 1:
/* 170 */         network.setId(readString());
        break;
    }
  }



  public void visitNetworkPolicy(NetworkPolicy networkPolicy) {
    byte tag;
/* 179 */     while ((tag = readTag()) != -1) {
/* 180 */       visitNetworkPolicyChildren(networkPolicy, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitNetworkPolicyChildren(NetworkPolicy networkPolicy, int _field, int _type) {
/* 185 */     switch (_field) {
      case 1:
/* 187 */         networkPolicy.setId(readString());
        break;
      case 2:
/* 190 */         networkPolicy.setTitle(readString());
        break;
      case 3:
/* 193 */         networkPolicy.setBlock(readBoolean());
        break;
      case 4:
/* 196 */         networkPolicy.setServerGroup(readString());
        break;
      case 33:
/* 199 */         if (_type == 1) {
/* 200 */           Network network = new Network();

/* 202 */           visitNetwork(network);
/* 203 */           this.m_linker.onNetwork(networkPolicy, network); break;
/* 204 */         }  if (_type == 2) {
/* 205 */           for (int i = readInt(); i > 0; i--) {
/* 206 */             Network network = new Network();

/* 208 */             visitNetwork(network);
/* 209 */             this.m_linker.onNetwork(networkPolicy, network);
          }
        }
        break;
    }
  }



  public void visitRouterConfig(RouterConfig routerConfig) {
    byte tag;
/* 220 */     if ((tag = readTag()) != -4) {
/* 221 */       throw new RuntimeException(String.format("Malformed payload, expected: %s, but was: %s!", new Object[] { Integer.valueOf(-4), Byte.valueOf(tag) }));
    }

/* 224 */     while ((tag = readTag()) != -1) {
/* 225 */       visitRouterConfigChildren(routerConfig, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitRouterConfigChildren(RouterConfig routerConfig, int _field, int _type) {
/* 230 */     switch (_field) {
      case 1:
/* 232 */         routerConfig.setBackupServer(readString());
        break;
      case 2:
/* 235 */         routerConfig.setBackupServerPort(readInt());
        break;
      case 3:
/* 238 */         routerConfig.setStartTime(readDate());
        break;
      case 4:
/* 241 */         routerConfig.setDomain(readString());
        break;
      case 5:
/* 244 */         routerConfig.setEndTime(readDate());
        break;
      case 33:
/* 247 */         if (_type == 1) {
/* 248 */           DefaultServer defaultServer = new DefaultServer();

/* 250 */           visitDefaultServer(defaultServer);
/* 251 */           this.m_linker.onDefaultServer(routerConfig, defaultServer); break;
/* 252 */         }  if (_type == 2) {
/* 253 */           for (int i = readInt(); i > 0; i--) {
/* 254 */             DefaultServer defaultServer = new DefaultServer();

/* 256 */             visitDefaultServer(defaultServer);
/* 257 */             this.m_linker.onDefaultServer(routerConfig, defaultServer);
          }
        }
        break;
      case 34:
/* 262 */         if (_type == 1) {
/* 263 */           NetworkPolicy networkPolicy = new NetworkPolicy();

/* 265 */           visitNetworkPolicy(networkPolicy);
/* 266 */           this.m_linker.onNetworkPolicy(routerConfig, networkPolicy); break;
/* 267 */         }  if (_type == 2) {
/* 268 */           for (int i = readInt(); i > 0; i--) {
/* 269 */             NetworkPolicy networkPolicy = new NetworkPolicy();

/* 271 */             visitNetworkPolicy(networkPolicy);
/* 272 */             this.m_linker.onNetworkPolicy(routerConfig, networkPolicy);
          }
        }
        break;
      case 35:
/* 277 */         if (_type == 1) {
/* 278 */           ServerGroup serverGroup = new ServerGroup();

/* 280 */           visitServerGroup(serverGroup);
/* 281 */           this.m_linker.onServerGroup(routerConfig, serverGroup); break;
/* 282 */         }  if (_type == 2) {
/* 283 */           for (int i = readInt(); i > 0; i--) {
/* 284 */             ServerGroup serverGroup = new ServerGroup();

/* 286 */             visitServerGroup(serverGroup);
/* 287 */             this.m_linker.onServerGroup(routerConfig, serverGroup);
          }
        }
        break;
      case 36:
/* 292 */         if (_type == 1) {
/* 293 */           Domain domain = new Domain();

/* 295 */           visitDomain(domain);
/* 296 */           this.m_linker.onDomain(routerConfig, domain); break;
/* 297 */         }  if (_type == 2) {
/* 298 */           for (int i = readInt(); i > 0; i--) {
/* 299 */             Domain domain = new Domain();

/* 301 */             visitDomain(domain);
/* 302 */             this.m_linker.onDomain(routerConfig, domain);
          }
        }
        break;
    }
  }



  public void visitServer(Server server) {
    byte tag;
/* 313 */     while ((tag = readTag()) != -1) {
/* 314 */       visitServerChildren(server, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitServerChildren(Server server, int _field, int _type) {
/* 319 */     switch (_field) {
      case 1:
/* 321 */         server.setId(readString());
        break;
      case 2:
/* 324 */         server.setPort(readInt());
        break;
      case 3:
/* 327 */         server.setWeight(readDouble());
        break;
    }
  }



  public void visitServerGroup(ServerGroup serverGroup) {
    byte tag;
/* 336 */     while ((tag = readTag()) != -1) {
/* 337 */       visitServerGroupChildren(serverGroup, (tag & 0xFF) >> 2, tag & 0x3);
    }
  }

  protected void visitServerGroupChildren(ServerGroup serverGroup, int _field, int _type) {
/* 342 */     switch (_field) {
      case 1:
/* 344 */         serverGroup.setId(readString());
        break;
      case 2:
/* 347 */         serverGroup.setTitle(readString());
        break;
      case 33:
/* 350 */         if (_type == 1) {
/* 351 */           GroupServer groupServer = new GroupServer();

/* 353 */           visitGroupServer(groupServer);
/* 354 */           this.m_linker.onGroupServer(serverGroup, groupServer); break;
/* 355 */         }  if (_type == 2) {
/* 356 */           for (int i = readInt(); i > 0; i--) {
/* 357 */             GroupServer groupServer = new GroupServer();

/* 359 */             visitGroupServer(groupServer);
/* 360 */             this.m_linker.onGroupServer(serverGroup, groupServer);
          }
        }
        break;
    }
  }

  private boolean readBoolean() {
    try {
/* 369 */       return ((this.m_in.readByte() == 1) ? Boolean.TRUE : Boolean.FALSE).booleanValue();
/* 370 */     } catch (IOException e) {
/* 371 */       throw new RuntimeException(e);
    }
  }

  private Date readDate() {
    try {
/* 377 */       return new Date(readVarint(64));
/* 378 */     } catch (IOException e) {
/* 379 */       throw new RuntimeException(e);
    }
  }

  private double readDouble() {
    try {
/* 385 */       return Double.longBitsToDouble(readVarint(64));
/* 386 */     } catch (IOException e) {
/* 387 */       throw new RuntimeException(e);
    }
  }

  private int readInt() {
    try {
/* 393 */       return (int)readVarint(32);
/* 394 */     } catch (IOException e) {
/* 395 */       throw new RuntimeException(e);
    }
  }

  private String readString() {
    try {
/* 401 */       return this.m_in.readUTF();
/* 402 */     } catch (IOException e) {
/* 403 */       throw new RuntimeException(e);
    }
  }

  private byte readTag() {
    try {
/* 409 */       return this.m_in.readByte();
/* 410 */     } catch (IOException e) {
/* 411 */       throw new RuntimeException(e);
    }
  }

  protected long readVarint(int length) throws IOException {
/* 416 */     int shift = 0;
/* 417 */     long result = 0L;

/* 419 */     while (shift < length) {
/* 420 */       byte b = this.m_in.readByte();
/* 421 */       result |= (b & 0x7F) << shift;
/* 422 */       if ((b & 0x80) == 0) {
/* 423 */         return result;
      }
/* 425 */       shift += 7;
    }

/* 428 */     throw new RuntimeException("Malformed variable int " + length + "!");
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/router/transform/DefaultNativeParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */